package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.font.ActivityAlertsTo;
import com.jeff.tianti.cms.entity.ActivityAlerts;
import com.jeff.tianti.cms.entity.ApplicationForMembership;
import com.jeff.tianti.cms.service.ActivityAlertsService;
import com.jeff.tianti.cms.service.ApplicationForMembershipService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/channel/members")//江苏频道_会员之家模块
@CrossOrigin(maxAge = 3600)
public class MembersOfTheFamilyController {
    @Autowired
    private ApplicationForMembershipService applicationForMembershipService;
    @Autowired
    private ActivityAlertsService activityAlertsService;

    /**
     * @Ceator:liuYI
     * @Describe:江苏频道_会员之家_点击申请
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult saveMembers(@RequestBody ApplicationForMembership appl) {
        appl.setAuditFlag(BaseEntity.AUDIT_FLAG_NOT);
        applicationForMembershipService.save(appl);
        return AjaxResult.setSuccessData(null);
    }

    /**
     * 活动快讯/会员之家通用接口
     * @Ceator:liuYI
     * @Describe:江苏频道_会员之家(会员名录,会员动态,会员服务)/活动快讯(协会快讯,重要活动,年度计划)列表
     */
    @RequestMapping(value = "/service", method = RequestMethod.POST)
    public AjaxResult activityAlertsList(@RequestBody ActivityAlertsTo actTo) {
        Page<ActivityAlerts> page = activityAlertsService.findByTo(actTo);
        return AjaxResult.setSuccessPageDate(page);
    }

    /**
     * 活动快讯/会员之家通用接口
     * @Ceator:liuYI
     * @Describe:江苏频道_活动快讯/会员之家_详情查看
     */
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public AjaxResult findActivityAlertsById(@RequestParam("id") String id) {
        ActivityAlerts activityAlerts = activityAlertsService.find(id);
        return AjaxResult.setSuccessData(activityAlerts);
    }
    /**
     *@Ceator:liuYI
     *@Describe:江苏频道_快讯/重要活动/会员之家模块数据
    */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public AjaxResult findAll() {
//        state    0->活动快讯 1->会员之家
//        type     活动快讯-> 0->协会快讯,1->重要活动 2->年度计划    会员之家-> 0->会员名录1->会员动态2->会员服务
        List<Map<String, Object>> list = activityAlertsService.queryActivityAlertsList(null);
        Map<String, List> map = new HashMap<>();
        List<Map<String, Object>> collect = list.stream().filter(t -> "0".equals(t.get("state")) && "0".equals(t.get("type"))).collect(Collectors.toList());
        map.put("xhkx", collect.size() < 4 ? collect : collect.subList(0, 4));
        List<Map<String, Object>> collect1 = list.stream().filter(t -> "0".equals(t.get("state")) && "1".equals(t.get("type"))).collect(Collectors.toList());
        map.put("zyhd", collect1.size() < 6 ? collect1 : collect1.subList(0, 6));
        List<Map<String, Object>> collect2 = list.stream().filter(t -> "1".equals(t.get("state")) && "0".equals(t.get("type"))).collect(Collectors.toList());
        map.put("hyml", collect2.size() < 5 ? collect2 : collect2.subList(0, 5));
        List<Map<String, Object>> collect3 = list.stream().filter(t -> "1".equals(t.get("state")) && "1".equals(t.get("type"))).collect(Collectors.toList());
        map.put("hydt", collect3.size() < 5 ? collect3 : collect3.subList(0, 5));
        List<Map<String, Object>> collect4 = list.stream().filter(t -> "1".equals(t.get("state")) && "2".equals(t.get("type"))).collect(Collectors.toList());
        map.put("hyfw", collect4.size() < 5 ? collect4 : collect4.subList(0, 5));
        return AjaxResult.setSuccessData(map);
    }
}
